package com.taru.wxhsh.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;

import com.taru.wxhsh.pojo.User;

import com.taru.wxhsh.service.UserLoginService;
import com.taru.wxhsh.service.UserService;
import com.taru.wxhsh.util.UidGenerators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserApi {

    Logger logger  = LoggerFactory.getLogger(UserApi.class);
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    UserService userService;


    @PostMapping("/api/login")
    public Object login(
            @RequestParam(value="username") String userUserName,
            @RequestParam(value="password") String userPassword

    ){
        JsonResult<User> result = null;
        try {
            User user =null;
            user = userLoginService.loginSelect(userUserName,userPassword);
            if (user!=null){
                result = new JsonResult<>(JResponseConstants.STATUS_OK,"登陆成功",user);
            }else {
                result = new JsonResult<>(JResponseConstants.STATUS_NO_DATA,"用户名或密码错误");
            }
        }catch (Exception ex){
            logger.error("未知异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"未知异常");
        }
        return result;
    }



    /**
     * 根据id查询用户用作修改
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/user/findById")
    public Object findById( @RequestParam(value="userId") String uid){
        JsonResult<User> result = null;
        try {
            User user = userService.selectByPrimaryKey(uid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"查询用户成功",user);
        }catch (Exception ex){
            logger.error("查询用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询用户异常");
        }
        return result;
    }



    /**
     * 查询用户列表 (仅供练习 )
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/api/user/list")
    @ResponseBody
    public Object  list(@RequestParam(value="pageNum",defaultValue = "1",required = false) Integer pageNum,
                        @RequestParam(value = "pageSize" ,defaultValue = "8" ,required = false) Integer pageSize){
        JsonResult<PageInfo<User>>   result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<User> users =userService.findUsersList(null,null);
            PageInfo<User>  pageInfo =new PageInfo<>(users,pageSize);
            result =new JsonResult<  PageInfo<User>>(JResponseConstants.STATUS_OK,"查询用户列表成功",pageInfo);
        }catch (Exception ex){
            logger.error("查询用户列表异常:{}",ex.getMessage());
            result =new JsonResult<  PageInfo<User>>(JResponseConstants.STATUS_ERROR,"查询用户列表异常");
        }
        return  result;


    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/user/add")
    public Object userAdd(User user){
        JsonResult<String> result = null;
        try {
            user.setUserId(UidGenerators.getUuid());
            userService.addUser(user);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"添加用户成功");
        }catch (Exception ex){
            logger.error("添加用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"添加用户异常");
        }
        return result;
    }






}
